package com.example.ndkimage;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.List;

public class MainActivity extends AppCompatActivity {


    private static int REQUESTCODE = 1;

    private ImageEncrypDecryp imageEncrypDecryp = new ImageEncrypDecryp();


    private Button btn_select, btn_encryp, btn_dncryp, btn_split,btn_merge;

    String path = "/storage/emulated/0/DCIM/Camera/IMG_20210501_171036.jpg";
    String[] files = {

            "/storage/emulated/0/DCIM/Camera/0",
            "/storage/emulated/0/DCIM/Camera/1",
            "/storage/emulated/0/DCIM/Camera/2"

    };

    String merge = "/storage/emulated/0/DCIM/Camera/merge.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_select = findViewById(R.id.btn_select);
        btn_encryp = findViewById(R.id.btn_encryp);
        btn_dncryp = findViewById(R.id.btn_dncryp);
        btn_split = findViewById(R.id.btn_split);
        btn_merge = findViewById(R.id.btn_merge);


        XXPermissions.with(this).permission(Permission.WRITE_EXTERNAL_STORAGE).permission(Permission.READ_EXTERNAL_STORAGE).request(new OnPermissionCallback() {
            @Override
            public void onGranted(List<String> permissions, boolean all) {
                if (all) {
                    btn_select.setVisibility(View.VISIBLE);
                    btn_encryp.setVisibility(View.VISIBLE);
                    btn_dncryp.setVisibility(View.VISIBLE);
                } else {
                    btn_select.setVisibility(View.GONE);
                    btn_encryp.setVisibility(View.GONE);
                    btn_dncryp.setVisibility(View.GONE);
                }
            }
        });

        btn_select.setOnClickListener(v -> {

            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("*/*");//无类型限制
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(intent, REQUESTCODE);

        });


        btn_encryp.setOnClickListener(v -> {


            String time = System.currentTimeMillis() + "";
            String name = path.substring(0, path.lastIndexOf("."));
            String type = path.substring(path.lastIndexOf("."));
            String des = name + time + type;

            long cueTime = System.currentTimeMillis();

            imageEncrypDecryp.imageEncryp(path, des);

            long diff = System.currentTimeMillis() - cueTime;
            Log.e("MainActivity", " btn_encryp " + diff);

        });

        btn_dncryp.setOnClickListener(v -> {
            String time = System.currentTimeMillis() + "";
            String name = path.substring(0, path.lastIndexOf("."));
            String type = path.substring(path.lastIndexOf("."));
            String des = name + time + type;
            long cueTime = System.currentTimeMillis();
            imageEncrypDecryp.imageDncryp(path, des);
            long diff = System.currentTimeMillis() - cueTime;
            Log.e("MainActivity", " btn_encryp " + diff);
        });


        btn_split.setOnClickListener(v -> {
            long cueTime = System.currentTimeMillis();
            imageEncrypDecryp.fileSplit(path, 3);
            long diff = System.currentTimeMillis() - cueTime;
            Log.e("MainActivity", " btn_encryp " + diff);
        });



        btn_merge.setOnClickListener(v -> {

            long cueTime = System.currentTimeMillis();
            imageEncrypDecryp.fileMerge(files,merge);

            long diff = System.currentTimeMillis() - cueTime;
            Log.e("MainActivity", " btn_encryp " + diff);
        });






    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            Uri uri = data.getData();
            path = ContentUriUtil.getPath(this, uri);
            Toast.makeText(this, path, Toast.LENGTH_LONG).show();
        }
    }


}